//
//  ZRWelcomeViewController.swift
//  Sina
//
//  Created by 张锐 on 16/3/1.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit
import SDWebImage
class ZRWelcomeViewController: UIViewController {
    
    override func loadView() {
        
        self.view = self.bgImageView
        
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
        
    }

    /**
    *  添加控件设置属性
    */
    func setupUI(){
    
        view.addSubview(photoImageView)
        
        view.addSubview(nameLabel)
        
        
        
        photoImageView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(view.snp_bottom).offset(-170)
            
            make.centerX.equalTo(view.snp_centerX)
            
            make.size.equalTo(CGSizeMake(85, 85))
            
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(view.snp_centerX)
            
        }
        
        let url = NSURL(string: "\(ZRTokenModelView.shareInstance.model?.profile_image_url ?? "")")
        
        if let Ourl = url {
            
            photoImageView.sd_setImageWithURL(Ourl, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
        
    }
    /**
     动画效果
     */
    func startAnimation(){
        
        photoImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-455)
            
            make.centerX.equalTo(view.snp_centerX)
            
            make.size.equalTo(CGSizeMake(85, 85))
        }
        
        UIView.animateWithDuration(1.5, delay: 0.3, usingSpringWithDamping: 0.7, initialSpringVelocity: 6, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                NSNotificationCenter.defaultCenter().postNotificationName("changeRootController", object: nil)
                
        }
        
        
        
        
    }
    
    /**
    *  懒加载控件
    */
    lazy var bgImageView: UIImageView = {
    
        let bgImageView = UIImageView()
        
        bgImageView.image = UIImage(named: "ad_background")
        
        return bgImageView
    
    }()
    
    lazy var photoImageView : UIImageView = {
    
        let photoImageView = UIImageView()
        
        photoImageView.image = UIImage(named: "avatar_default_big")
        
        
        
        photoImageView.layer.cornerRadius = 170/4
        
        photoImageView.layer.masksToBounds = true
        
        photoImageView.layer.borderColor = UIColor.darkGrayColor().CGColor
        
        photoImageView.layer.borderWidth = 2
        
        return photoImageView
    
    }()
    
    lazy var nameLabel : UILabel = {
    
        let nameLabel = UILabel()
        
        nameLabel.text = "\(ZRTokenModelView.shareInstance.model?.screen_name ?? "")欢迎归来"
        
        nameLabel.textAlignment = .Center
        
        nameLabel.textColor = UIColor.darkGrayColor()
        
        nameLabel.font = UIFont.systemFontOfSize(15)
        
        return nameLabel
    
    }()

}
